package com.delaphone.flocash.web.action;

import com.delaphone.flocash.model.FCards;
import com.delaphone.flocash.model.FUser;
import com.delaphone.flocash.service.BankService;
import com.delaphone.flocash.service.UserService;
import com.delaphone.flocash.web.util.Validate;
import com.delaphone.flocash.web.bean.SelectList;
import com.opensymphony.xwork2.Action;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author dongtv
 */
public class AddCardAction extends BaseAction {

    private ArrayList<SelectList> types;
    private ArrayList<SelectList> months;
    private ArrayList<SelectList> years;
    private String type;
    private String number;
    private String expire_year;
    private Integer expire_month;
    private String cvc;
    private UserService userService;
    private BankService bankService;
    private String message;

    public String index() {
        return Action.INPUT;
    }

    public String addCards() {
        validateField();
        if (getActionErrors().size() > 0) {
            return Action.INPUT;
        }
        FUser u = userService.getUserByEmail(user.getEmail());
        FCards card = new FCards();
        card.setUserId(u.getId());
        card.setNumber(getNumber());
        card.setType(getType());
        card.setExpireMonth(getExpire_month());
        card.setExpireYear(getExpire_year());
        card.setActive("no");
        card.setCvc(getCvc());
        card.setVerifyReq(true);
        card.setDateCreated(new Date());
        bankService.createCard(card);
        setMessage(getText("user.manage_card.addCard"));
        return Action.SUCCESS;
    }

    public BankService getBankService() {
        return bankService;
    }

    public void setBankService(BankService bankService) {
        this.bankService = bankService;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public ArrayList<SelectList> getTypes() {
        types = new ArrayList<SelectList>();
        types.add(new SelectList("", "Select Type"));
        types.add(new SelectList("mastercard", "MasterCard"));
        types.add(new SelectList("visa", "Visa"));
        return types;
    }

    public void setTypes(ArrayList<SelectList> types) {
        this.types = types;
    }

    public ArrayList<SelectList> getMonths() {
        months = new ArrayList<SelectList>();
        String[] ms = new String[]{"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};
        for (int i = 0; i < ms.length; i++) {
            months.add(new SelectList(ms[i], ms[i]));
        }
        return months;
    }

    public void setMonths(ArrayList<SelectList> months) {
        this.months = months;
    }

    public ArrayList<SelectList> getYears() {
        years = new ArrayList<SelectList>();
        int y = new Date().getYear() + 1900;
        for (int i = 0; i < 8; i++) {
            years.add(new SelectList(String.valueOf(y + i), String.valueOf(y + i)));
        }
        return years;
    }

    public void setYears(ArrayList<SelectList> years) {
        this.years = years;
    }

    public Integer getExpire_month() {
        return expire_month;
    }

    public void setExpire_month(Integer expire_month) {
        this.expire_month = expire_month;
    }

    public String getExpire_year() {
        return expire_year;
    }

    public void setExpire_year(String expire_year) {
        this.expire_year = expire_year;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getCvc() {
        return cvc;
    }

    public void setCvc(String cvc) {
        this.cvc = cvc;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public void validateField() {
        if (Validate.isEmptyString(getNumber())) {
            addActionError(getText("user.add_card.number.null"));
        } else if (!Validate.isNumber(getNumber())) {
            addActionError(getText("user.add_card.number.format"));
        } else if (Validate.isEmptyString(getCvc())) {
            addActionError(getText("user.add_card.cvc.null"));
        } else if (getType().equals("")) {
            addActionError(getText("user.add_card.type.null"));
        }
    }
}
